package com.aftersale.demo.service.impl;

import com.aftersale.demo.entity.Problem;
import com.aftersale.demo.entity.Result;
import com.aftersale.demo.utils.MyUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aftersale.demo.entity.Project;
import com.aftersale.demo.service.ProjectService;
import com.aftersale.demo.mapper.ProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project>
    implements ProjectService{
    @Autowired
    ProjectMapper projectMapper;

    @Override
    public Result addproject(Project project) {
        LambdaQueryWrapper<Project> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Project::getName,project.getName());
        Project dbProject=projectMapper.selectOne(queryWrapper);
        if(Objects.isNull(dbProject)){
            project.setCreateTime(MyUtil.getNow());
            projectMapper.insert(project);
            return Result.success("添加项目成功");
        }else {
            return Result.error("401","项目已存在");
        }

    }

    @Override
    public Result deleteProject(int projectId) {
        Project dbProject =projectMapper.selectById(projectId);
        if(Objects.isNull(dbProject)){
            return Result.error("401","项目不存在");
        }else{
            projectMapper.deleteById(projectId);
            return Result.success("删除成功");
        }
    }

    @Override
    public Result getProject(int userId,Integer pageNum,Integer pageSize) {
        Page<Project> myProject=projectMapper.getProject(new Page<>(pageNum,pageSize),userId);
        if(myProject.getTotal()==0){
            return Result.error("401","您还未购买任何产品");
        }else{
            return Result.success("获取成功",myProject);
        }

    }

    @Override
    public Result selectProject(int userId, String name,Integer pageNum,Integer pageSize) {
        Page<Project> project=projectMapper.selectMyProject(userId,name,new Page<>(pageNum,pageSize));
        return Result.success(project);
    }

    @Override
    public Result selectProjects(String name, Integer pageNum, Integer pageSize) {
        QueryWrapper<Project> projectQueryWrapper=new QueryWrapper<>();
        projectQueryWrapper.like("name",name);
        Page<Project> projectPage=page(new Page<>(pageNum,pageSize),projectQueryWrapper);
        return Result.success(projectPage);
    }

    @Override
    public Result updateProject(Project project) {
        LambdaQueryWrapper<Project> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Project::getName,project.getName());
        Project dbProject=projectMapper.selectOne(queryWrapper);
        if(project.getName().equals(projectMapper.selectById(project.getId()).getName())||Objects.isNull(dbProject)){
            project.setUpdateTime(MyUtil.getNow());
            projectMapper.updateProject(project);
            return Result.success("修改成功");
        }else {
            return Result.error("401","项目名已存在");
        }
    }


}




